package org.raymond.iworks.study.example;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.net.url.UrlQuery;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;

import org.raymond.iworks.core.utils.RequestUtil;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class StringTest {

    @Test
    public void test1(){
        String s1 = "\u0020";
        String s2 = "\u00a0";
        String s3 = "\u3000";
        System.out.println("s1 is whitespace: "+StringUtils.isWhitespace(s1));
        System.out.println("s2 is whitespace: "+StringUtils.isWhitespace(s2));
        System.out.println("s3 is whitespace: "+StringUtils.isWhitespace(s3));
    }

    @Test
    public void test2(){
        Map<String, Integer> userListMap = new HashMap<>();
        userListMap.put("size", 10);
        userListMap.put("current", RandomUtil.randomInt(9));
        String urlQuery = UrlQuery.of(userListMap).build(StandardCharsets.UTF_8);
        System.out.println(urlQuery);
    }

    @Test
    public void test01(){
        Set<String> all = Set.of("APP001", "APP002", "APP003", "APP004", "APP101");
        Set<String> exclude = Set.of("APP002", "APP101");
        List<String> remain = all.stream().filter(e->!exclude.contains(e)).collect(Collectors.toList());
        System.out.printf("remain: %s, exclude: %s", remain, exclude);
    }

    @Test
    public void urlToMap(){
        String s = "current=0&size=10";
        Map map = UrlQuery.of(s, StandardCharsets.UTF_8).getQueryMap();
    }

    @Test
    public void jsonArrTo(){
        String s = "[{column:\"update\",asc:true}]";
        List<Object> arr = JSONUtil.toList(s, Object.class);
        System.out.println(arr.get(0));
    }

    @Test
    public void contain(){
        String s = "icloud/admin/user/exportExcel";
        boolean r = RequestUtil.ignoreDownFile(s);
        System.out.println(r);
    }

    @Test
    public void ex(){
        try{
            throw new Exception("hello exec");
        }catch(Exception e){
            StackTraceElement ste = e.getStackTrace()[0];
            System.out.println(ste.toString()+":"+ExceptionUtil.getRootCauseMessage(e));
        }
    }

    @Test
    public void split(){
        String speed = "reset:0";
        if(speed.startsWith("reset") && speed.indexOf(":")==5){
            String[] speedStrs = speed.split(":");
            System.out.println("reset:"+speedStrs[1]);
        }
    }

    @Test
    public void round(){
        BigDecimal o = BigDecimal.ONE;
        BigDecimal t = new BigDecimal("0.01");
        BigDecimal r = o.multiply(t).setScale(0, RoundingMode.HALF_UP);
        System.out.println(r);
    }
}
